package cn.etynit.org.controller;

import cn.etynit.basic.anno.PreAuthorize;
import cn.etynit.basic.config.BussinessException;
import cn.etynit.basic.util.BaiduAuditUtils;
import cn.etynit.basic.util.ExcelUtil;
import cn.etynit.org.domain.ShopAuditLog;
import cn.etynit.org.service.IShopService;
import cn.etynit.org.domain.Shop;
import cn.etynit.org.query.ShopQuery;
import cn.etynit.basic.util.AjaxResult;
import cn.etynit.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
@PreAuthorize
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "添加或修改店铺",sn = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
            // AI验证
            if(!BaiduAuditUtils.TextCensor(shop.getName())){
                throw new BussinessException("店铺名称不合规!");
            }
            if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
                throw new BussinessException("店铺logo不合规！");
            }
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除店铺",sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
            shopService.batchDel(ids);
            return AjaxResult.me().success();
    }


	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "查询店铺",sn = "shop:List")
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "查询店铺",sn = "shop:List")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }

    /**
     * 店铺入住接口
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return AjaxResult.me().success();
    }

    /**
     * 店铺入驻审核
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog auditLog) throws MessagingException {
        shopService.auditPass(auditLog);
        return AjaxResult.me().success();
    }

    /**
     * 店铺激活
     * @param id
     * @return
     * @throws MessagingException
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) throws MessagingException {
        shopService.active(id);

        return AjaxResult.me().success();
    }

    /**
     * 导出
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse res){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,"","sheet1",Shop.class,"店铺列表.xlsx",
                res);
    }

    /**
     * 导入
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shops.forEach(System.out::println);
    }

    /**
     * 报表
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return shopService.echarts();
    }


    /**
     * 审核
     * @return
     */
    @PostMapping("/audit")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        System.out.println(shopAuditLog);
        shopService.audit(shopAuditLog);
        return AjaxResult.me().success();
    }

    /**
     * 审核通过
     * @return
     */
//    @PostMapping("/audit/reject")
//    public AjaxResult reject(ShopAuditLog shopAuditLog){
//        shopService.reject(shopAuditLog);
//        return AjaxResult.me().success();
//    }
}
